package researchServlets;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.LoginSystemRemote;

public class EraseUserServlet extends HttpServlet {

	private static final long serialVersionUID = -3867365856174819553L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		eraseUser(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		eraseUser(request, response);
	}

	private void eraseUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		boolean erased = false;
		try {
			Context jndiCtx = new InitialContext();
			LoginSystemRemote loginSystemRemote = (LoginSystemRemote) jndiCtx
					.lookup("Progetto/LoginSystem/remote");
			erased = loginSystemRemote.eraseUser(Integer.parseInt(request
					.getParameter("idu")));
			if (erased)
				//utente eliminato
				request.getSession().setAttribute("erased", "true");
			else
				//utente non eliminato
				request.getSession().setAttribute("erased", "false");

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Errore nella servlet");
		}
		response.sendRedirect("/ProgettoWeb/Research/UserErased.jsp?erased=false");
	}
}